package aad.storyboard.controller;


import java.util.Observable;

import aad.storyboard.model.AudioPlayingState;
import aad.storyboard.model.AudioState;
import android.content.Context;
import android.media.MediaPlayer;

/**
 * Is being observed by the StoryTimer that retrieves its current position in order to notify its observers Constitutes the context for the AudioStates in the State pattern
 
 */
public class AudioPlayer extends Observable implements  Runnable, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener{
	private Context context;
		
	private static int fileId;
	private static MediaPlayer mPlayer;
	

	
	/**
	 * declear reference instances to hold all state objects	
	 */
	private AudioState playingState;	
	private AudioState pausedState;	
	private AudioState stoppedState;
	
	/**
	 * define a reference to hold current state of player	
	 */
	private AudioState currentState;	
	
	private boolean init=true;
	
	private boolean playing=true;

	

		
	private static final String TAG = "AudioPlayerTest";

	/**
	 * Singleton
	 */
	private static AudioPlayer inst = new AudioPlayer ();

	public void setfileId (int id)
	{
		AudioPlayer.fileId=id;
	}

	private AudioPlayer(){
					
		playingState = new AudioPlayingState(this);		
		pausedState = new AudioPausedState(this);		
		stoppedState = new AudioStoppedState(this);
		
		
		currentState = stoppedState;		
		
	}
	
	/**
	 * SingleTon
	 * @return
	 */
	public static AudioPlayer getInstance()	{		
		return inst;
	}
	
	
    public void playAudio(){        
        currentState.playAudio();
    }
    
    
    public void pauseAudio(){        
        currentState.pauseAudio();
    }
    
    
    public void stopAudio(){        
        currentState.stopAudio();
    }
    
  
    public void seekToPosition(int pos){        
        currentState.seekToPosition(pos);
    }
    
     
    public void setNewState(AudioState state){
        System.out.println("--- State Changed From "+currentState+" to "+state);       
        currentState = state;
        setChanged();
        notifyObservers();
    }
    
    public AudioState getState(){
        return currentState;
    }
    
    
    public AudioState getPlayingState(){
        return playingState;
    }
    
    
    public AudioState getPausedState(){
        return pausedState;
    }
    
    public AudioState getStoppedState(){
        return stoppedState;
    }
    
		
	
	void setContext(Context c) {
		this.context=c;
		
	}

	public void initializeMediaPlayer() {
		try {			
			mPlayer = MediaPlayer.create(context, fileId);			
			mPlayer.setOnPreparedListener(inst);			    
			    			
			(new Thread(inst)).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	public int getDuration()
	{
		return mPlayer.getDuration();	
	}
	
	public MediaPlayer getPlayer()
	{
		return mPlayer;	
	}

	
	@Override
	public void run() {
		while(playing)
		{
			try {
				if(!init)
		    	 {setChanged();
		    	 notifyObservers();	
		    	 }
				Thread.sleep(10);
			} catch (InterruptedException e) {				
				e.printStackTrace();
			}
	     
		}
	}
		
	
	
	@Override
	public void onPrepared(MediaPlayer mp) {
		if (init)	{	
			//correction=getCurrentPosition();
			System.out.println("Ich starte "+"Duration "+mPlayer.getDuration()+"Current"+getCurrentPosition());
			setChanged();
			notifyObservers();
			init=false;
		}			
	}
	
	

	public int getCurrentPosition()
	{
		return AudioPlayer.mPlayer.getCurrentPosition();		
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		this.stopAudio();
		this.playing = false;
		ControllerManager.getInstance().getActivity().setContentView(MenuViewProvider.getInstance().getFinalView());
		
	}

		

	
}
